package demo.topic15;

import org.junit.Test;

import java.util.EnumMap;
import java.util.EnumSet;

public class EnumDemo {
    public Color colorEnum;

    @Test
    public void test1()  {
        colorEnum = Color.RED;
        System.out.println(colorEnum);
        colorEnum = Color.YELLOW;
        System.out.println(colorEnum);
        colorEnum = Color.GREEN;
        System.out.println(colorEnum);
    }

    /**
     * EnumSet：JDK1.5 新增
     * EnumMap：JDK1.5 新增
     */
    @Test
    public void test2()  {
        EnumSet<Color> set = EnumSet.allOf(Color.class);
        /**
         * RED
         * YELLOW
         * GREEN
         */
        for (Color color : set) {
            System.out.println(color);
        }

        EnumMap<Color, Object> map = new EnumMap<>(Color.class);
        map.put(Color.RED, "#f00");
        map.put(Color.YELLOW, "#ff0");
        map.put(Color.GREEN, "#0f0");
        /**
         * { key: RED, value: #f00 }
         * { key: YELLOW, value: #ff0 }
         * { key: GREEN, value: #0f0 }
         */
        map.forEach((key, value) -> {
            System.out.println("{ key: "+key + ", value: "+value+" }");
        });
    }

    /**
     * 带构造函数的枚举，实现接口
     */
    @Test
    public void test3() {
        EnumSet<Color2> set = EnumSet.allOf(Color2.class);
        /**
         * 红色 -> name: RED, ordinal: 0
         * 黄色 -> name: YELLOW, ordinal: 1
         * 绿色 -> name: GREEN, ordinal: 2
         */
        for (Color2 color : set) {
            System.out.println(color.getColor() + " -> " + "name: " + color.name() + ", ordinal: " + color.ordinal() );
        }

        System.out.println(Color2.GREEN.getColor()); // 绿色
    }

    @Test
    public void test4() {
        Singleton.SINGLETON.method();
    }
}
